package com.stylefeng.guns.modular.system.task;

import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.model.Gpdata;
import com.stylefeng.guns.modular.system.service.IGpdataService;

//@Service
public class RealPriceImportTask implements InitializingBean {
	
	private static final Logger log = LoggerFactory.getLogger(RealPriceImportTask.class);
	
	private static final HttpClientUtil clientUtil = new HttpClientUtil();

	private static Map<String, String> real_price = new ConcurrentHashMap<String, String>();
	
	private static ScheduledExecutorService scheduledExecutorService =   Executors.newScheduledThreadPool(200);

	private static List<getNewPrice> allTask = new ArrayList<getNewPrice>();
	
	private static List<ScheduledFuture<?>> allScheduledFuture = new ArrayList<ScheduledFuture<?>>();
	
	@Autowired
	private IGpdataService gpdataService;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		real_price.clear();
		List<Gpdata> gpdatas = gpdataService.selectList(null);
		for (Gpdata gpdate : gpdatas) {
			real_price.put(gpdate.getgDm(), "0");
			allTask.add(new getNewPrice(gpdate.getgDm()));
		}
	}
	
	public void start(){
		for(getNewPrice v : allTask){
			ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(v, 0, 5, TimeUnit.SECONDS);
			allScheduledFuture.add(scheduledFuture);
		}
		System.out.println("ccc");
	}
	
	public void stop(){
		for(ScheduledFuture<?> future : allScheduledFuture){
			if (!future.isCancelled()) {  
				future.cancel(false);  
            } 
		}
	}
	
	class getNewPrice implements Runnable{

		private String gdm;
		
		public getNewPrice(String gdm){
			this.gdm = gdm;
		}
		 
		@Override
		public void run() {
			String url = ImportContants.getRealNewPrice(gdm);
			HttpGet httpGet = HttpClientUtil.getGetRequest();
			try {
				URL http = new URL(url);
				URI uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
				httpGet.setURI(uri);
				String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
				if(StringUtils.isNotEmpty(json)){
					String[] zdfs = StringUtils.split(StringUtils.substringBetween(json, "(", ")"));
					real_price.put(gdm, StringUtils.split(zdfs[0],",")[7]);
				}
			} catch (Exception e) {
				log.error("get real new price is error!" + e);
			}finally{
				httpGet.abort();
			}
		}
		
	}
}
